<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-cascader
        v-model="idcCabinetQuery"
        :options="idcOptions"
        :props="idcProp"
        clearable
        style="width: 180px"
        class="filter-item"
        placeholder="请选择机柜"
        @active-item-change="getCabinetList"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">{{ $t('table.add') }}</el-button>
    </div>

    <el-table v-loading="listLoading" :key="tableKey" :data="list" element-loading-text="加载中" border fit highlight-current-row style="width: 100%">
      <el-table-column :label="$t('table.id')" align="center" width="65">
        <template slot-scope="scope">
          <span>{{ scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.idcName')" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.idcName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.cabinetCode')" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.cabinetName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.name')" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.startu')" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.startu }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.endu')" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.endu }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.createTime')" width="180px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.creator')" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.creator }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.updateTime')" width="180px">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.updator')" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.updator }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" fixed="right" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
          <el-button v-if="scope.row.status!='deleted'" size="mini" type="danger" @click="handleDelete(scope.row)">{{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <el-form-item :label="$t('table.cabinetCode')">
          <el-cascader
            v-model="idcCabinet"
            :options="idcOptions"
            placeholder="请选择机房/机柜"
            @active-item-change="getCabinetList"/>
        </el-form-item>
        <el-form-item :label="$t('table.name')" prop="name">
          <el-input v-model="temp.name"/>
        </el-form-item>
        <el-form-item :label="$t('table.startu')" prop="startu">
          <el-input-number v-model="temp.startu" :min="0" :max="1000"/>
        </el-form-item>
        <el-form-item :label="$t('table.endu')" prop="endu">
          <el-input-number v-model="temp.endu" :min="0" :max="1000"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">{{ $t('table.confirm') }}</el-button>
        <el-button v-else type="primary" @click="updateData">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel"/>
        <el-table-column prop="pv" label="Pv"/>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">{{ $t('table.confirm') }}</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { fetchList, fetchIdc, fetchCabinet, create, update, del } from '@/api/rack'

export default {
  name: 'ComplexTable',
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        idcId: undefined,
        cabinetId: undefined,
        sort: '+id'
      },
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      idcOptions: [],
      cabinetOptions: [],
      idcCabinet: [],
      idcCabinetQuery: [],
      showReviewer: false,
      temp: {
        id: undefined,
        name: '',
        startu: '',
        endu: '',
        idcId: '',
        cabinetId: ''
      },
      idcProp: {
        value: 'id',
        label: 'name',
        children: 'cabinets'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '新增'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        startu: [{ required: true, message: '起始U是必填项', trigger: 'blur' }],
        endu: [{ required: true, message: '结束U是必填项', trigger: 'blur' }],
        name: [{ required: true, message: '机架名称是必填项', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
    this.getIdcList()
  },
  methods: {
    getList() {
      this.listLoading = true
      if (this.idcCabinetQuery.length > 0) {
        this.listQuery.idcId = this.idcCabinetQuery[0]
        this.listQuery.cabinetId = this.idcCabinetQuery[1]
      } else {
        this.listQuery.idcId = ''
        this.listQuery.cabinetId = ''
      }
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    getIdcList() {
      fetchIdc().then(response => {
        this.idcOptions = response.data.records
      })
    },
    getCabinetList(val) {
      fetchCabinet({ idcId: parseInt(val) }).then(response => {
        for (var i = 0; i < this.idcOptions.length; i++) {
          if (parseInt(val) === this.idcOptions[i].id) {
            this.idcOptions[i].cabinets = response.data
            break
          }
        }
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleDelete(row) {
      this.$confirm('此删除操作不可还原, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row).then(response => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        name: '',
        comment: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.idcId = this.idcCabinet[0]
          this.temp.cabinetId = this.idcCabinet[1]
          create(this.temp).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.idcId = this.idcCabinet[0]
          this.temp.cabinetId = this.idcCabinet[1]
          const tempData = Object.assign({}, this.temp)
          update(tempData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>
